package com.erp.ae.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.erp.ae.domain.enums.AliExpressInterfaceEnum;
import com.erp.ae.service.IAeSellerStoreService;
import com.global.iop.domain.Protocol;
import com.ruoyi.common.core.domain.R;
import com.erp.ae.utils.AeApiUtils;
import com.erp.ae.utils.AeApiUtils;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 商户订单Service业务层处理
 *
 * @author erp
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class AeSellerStoreServiceImpl implements IAeSellerStoreService {


    @Override
    public R<Object> querySellerStoreInfo(String storeId) {
        //生成参数Map
        //获取店铺的accessToken
        String accessToken = AeApiUtils.getToken(storeId);
        System.out.println("*************accessToken:"+accessToken);
        if (accessToken == null) {
            R.fail("店铺未授权");
        }
        String msg =
                AeApiUtils.sendRequest(
                        AliExpressInterfaceEnum.getProfile.getCode(),
                        Protocol.TOP,
                        accessToken
                );
        System.out.println("*************storeProfile:"+msg);
        if (!msg.contains("MissingParameter")) {
            return R.ok(JSONObject.parseObject(msg).getJSONObject("aliexpress_merchant_profile_get_response").getString("profile"));
        } else {
            return R.fail("发送失败");
        }
    }

    @Override
    public R<Object> storeDiagnosis(Long storeId) {
        String accessToken = AeApiUtils.getToken(storeId);
        if (accessToken == null) {
            R.fail("店铺未授权");
        }
        String msg =
                AeApiUtils.sendRequest(
                        AliExpressInterfaceEnum.storeDiagnosis.getCode(),
                        Protocol.TOP,
                        accessToken
                );
        System.out.println(msg);
        return R.ok(msg);
    }

    @Override
    public R<Object> queryGlobalSellerRelation(String storeUserName) {
//        String accessToken = AeApiUtils.getToken(storeUserName);
        String accessToken = "50000101522ceDtbirexQG6Bft7LVwkiIQ102d3748lVFxyDcZHPtAPPYS1PKHIX3VKm";
        if (accessToken == null) {
            R.fail("店铺未授权");
        }
        String msg =
                AeApiUtils.sendRequestHttpMethodIsGet(
                        AliExpressInterfaceEnum.queryGlobalSellerRelation.getCode(),
                        Protocol.TOP,
                        accessToken
                );
        System.out.println(msg);
        return R.ok(msg);
    }
}
